<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { userLoginService } from '@/api/user.js'
import { useUserStore } from '@/stores'

const router = useRouter()
// const isRegister = ref(false)
const role = ref('学生')
const roleList = ['学生', '老师', '管理员']
const form = ref()

//提交的from对象
const fromModel = ref({
  username: '',
  password: ''
})

const rules = ref({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
})
watch(role, () => {
  fromModel.value = {
    username: '',
    password: ''
  }
})

const userStore = useUserStore()
const handleLogin = async () => {
  await form.value.validate()
  //============================================{
  // const res = await userLoginService(fromModel.value)
  userStore.setToken('res.data.token')
  ElMessage.success('登录成功')
  // const res = await axios.post('http://big-event-vue-api-t.itheima.net/api/login', {
  //   username: fromModel.value.username,
  //   password: fromModel.value.password
  // })
  //============================================}
  // console.log(res)
  if (form.value) {
    // console.log('submit!')
    // console.log(role.value)
    if (role.value === '学生') {
      console.log('学生登录')
      router.push('/student/')
    } else if (role.value === '老师') {
      console.log('老师登录')
      router.push('/teacher/')
    } else if (role.value === '管理员') {
      console.log('管理员登录')
      router.push('/administrators/')
    }
  } else {
    console.log('error submit!')
  }
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg">
      <h1 class="title">高校创新创业项目管理与学分认定系统</h1></el-col
    >
    <el-col :span="6" :offset="3" class="form">
      <el-form :model="fromModel" :rules="rules" ref="form" size="large" autocomplete="off">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item>
          <el-segmented
            v-model="role"
            :options="roleList"
            block
            style="background-color: #fff; width: 100%; height: 40px"
          />
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            :rules="rules.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
            v-model="fromModel.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :rules="rules.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            v-model="fromModel.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="primary" auto-insert-space @click="handleLogin()"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background: url('https://picsum.photos/2880') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 50%;
      height: 100%;
      background: rgba(0, 0, 0, 0.3);
      border-radius: 0 20px 20px 0;
    }
    .title {
      position: absolute;
      top: 10%;
      left: 25%;
      transform: translate(-50%, -50%);
      color: #ffffff; /* 标题颜色 */
      font-size: 2rem; /* 标题字体大小 */
      text-align: center;
      z-index: 1; /* 确保标题在半透明图层之上 */
    }
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
